<template>
	<!-- 动态详情 -->
	<view class="DynamicDetails">
		<view class="DynamicDetails-main">
			<view class="empty"></view>
			<view class="main">
				<view class="main-header">
					<view class="main-header-left">
						<view class="">
							<image src="https://img2.baidu.com/it/u=1790834130,1952230725&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"
								mode=""></image>
						</view>
						<view class="">
							<text>平台</text>
							<text>发布</text>
						</view>
					</view>
					<view class="main-header-right">
						<text>{{ form.create_time }}</text>
					</view>
				</view>
				<view class="main-content">
					<view class="label">
						{{ form.title }}
					</view>
					<view class="main-content-main">
						<rich-text :nodes="form.content"></rich-text>
					</view>
					<view class="main-content-image">
						<image :src="form.image" mode="aspectFit"></image>
					</view>
				</view>
				<view class="CommentCollection">
					<view class="" @click="eventLike()">
						<image v-if="form.is_like" src="../static/xiaoyutong/likeSelected.png"></image>
						<image v-else src="../static/xiaoyutong/like.png"></image>
						<text>{{ form.like_num }}</text>
					</view>
					<view class="">
						<image src="../static/xiaoyutong/comment.png" mode=""></image>
						<text>{{ form.count }}</text>
					</view>
				</view>
			</view>
			<view class="comment">
				<view class="comment-header">
					<text></text>
					<text>评论 </text>
					<text>{{ form.count }}</text>
				</view>
				<view class="comment-content">
					<view class="comment-content-item">
						<view class="comment-title" v-for="item in commentList" :key="item.id">
							<view class="comment-title-left">
								<view class="userImage">
									<image :src="item.headImage" mode=""></image>
								</view>
								<view class="user">
									<view class="userName">
										{{ item.member_info.nickname || item.member_info.mobile }}
									</view>
									<view class="dateTime">
										<text>{{ item.create_time }}</text>
									</view>
									<view class="comment-title-main">
										{{ item.content }}
									</view>
								</view>
							</view>
							<!-- <view class="comment-title-right">
								回复
							</view> -->
						</view>
						<!-- <view class="comment-main">
							<view class="comment-main-left">
								<text>ALAN LOPEZ</text>
								<text>感谢支持，哈哈。</text>
							</view>
							<view class="comment-main-right">
								<text>2023-04-14</text>
								<text>12:00</text>
							</view>
						</view> -->
					</view>
				</view>
			</view>
		</view>
		<view class="footer">
			<view class="footer-left">
				<input type="text" v-model="comment" placeholder="发表你的评论">
			</view>
			<view class="footer-right" @click="commentAdd">
				发送
			</view>
		</view>
	</view>
</template>

<script>
import likeSelected from '@/static/xiaoyutong/likeSelected.png'
import like from '@/static/xiaoyutong/like.png'
import mySelected from '@/static/mySelected.png'
const $api = require('@/utils/request.js').API

export default {
	data() {
		return {

			id: null,
			form: {},
			// 评论内容
			comment: '',
			// 评论列表
			commentList: [],
			// 评论总页数
			page_count: 1,
			// 当前页
			page: 1,
			// 用户细腻
			userInfo: {
				member_info: {
					nickName: '',
				},
				headImage: '',
				create_time: '',
				content: ''
			}
		}
	},
	onLoad(options) {

		this.id = options.id
		this.getInformationDetail()
		this.getCommentList()
		this.getUserInfo()
	},
	// 触底
	onReachBottom() {
		if (this.page == this.page_count) {
			uni.showToast({
				title: '没有更多了',
				icon: 'none'
			})
		} else {
			this.page = this.page + 1
			this.getCommentList()
		}
	},
	methods: {
		// 获取详情
		getInformationDetail() {
			$api.informationDetail({ id: this.id }).then(res => {
				this.form = res.data.data
				this.form.image = this.$image + this.form.image
				this.form.count = res.data.data.comment_list.count
			})
		},
		// 获取评论列表
		getCommentList() {
			let obj = {
				page: this.page,
				information_id: this.id,
			}
			$api.InformationCommentList(obj).then(res => {
				this.page_count = res.data.data.page_count
				let list = res.data.data.list
				list.forEach(item => {
					if (item.member_info.headimg) {
						item.headImage = this.$image + item.member_info.headimg
					} else {
						item.headImage = mySelected
					}
				})
				if (this.page == 1) {
					this.commentList = list
				} else {
					this.commentList = [...this.commentList, ...list]
				}
			})
		},
		// 获取用户信息
		getUserInfo() {
			$api.postUserInformation().then(res => {
				let { mobile, nickname, headimg } = res.data.data
				this.userInfo.member_info.nickname = nickname || mobile
				this.userInfo.headImage = headimg || mySelected
			})
		},
		// 评论
		commentAdd() {
			if (this.comment == '') {
				uni.showToast({
					title: '请输入评论内容',
					icon: "none",
					duration: 2000,
				});
				return;
			}
			let obj = {
				information_id: this.id,
				content: this.comment
			}
			$api.InformationCommentAdd(obj).then(res => {
				let timestamp = Date.now();
				this.userInfo.create_time = this.$options.filters.formatTime(timestamp / 1000)
				this.userInfo.content = this.comment
				this.commentList.unshift(this.userInfo)
				this.comment = ''
			})
		},
		// 点赞
		eventLike() {
			if (this.form.is_like) {
				this.form.is_like = false
				this.form.like_num = this.form.like_num - 1
			} else {
				this.form.is_like = true
				this.form.like_num = this.form.like_num + 1
			}
			$api.informationLike({ id: this.form.id }).then(res => {

			})
		},
	}

}
</script>

<style lang="less" scoped>
.DynamicDetails {
	position: relative;
	width: 100vw;
	background: #F9F9F9;

	.empty {
		height: 7rpx;
	}

	.DynamicDetails-main {
		width: 100vw;
		overflow: hidden;
		overflow-y: scroll;
	}

	.main {
		width: 750rpx;
		// height: 639rpx;
		background: #FFFFFF;
		padding: 0 29rpx 0 30rpx;

		.main-header {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 32rpx 0 34rpx 0;

			.main-header-left {
				display: flex;
				align-items: center;

				image {
					width: 50rpx;
					height: 50rpx;
					// background: #91A6D4;
					border-radius: 50%;
				}

				view:first-child {
					padding-right: 9rpx;
				}

				view:last-child {
					font-size: 31rpx;
					font-family: PingFang SC;
					font-weight: 400;
					color: #333333;

					text:last-child {
						font-size: 30rpx;
						font-family: PingFang SC;
						font-weight: 400;
						color: #888888;
						padding-left: 12rpx;
					}
				}
			}

			.main-header-right {
				font-size: 22rpx;
				font-family: PingFang SC;
				font-weight: 400;
				color: #666666;

				text:last-child {
					padding-left: 20rpx;
				}
			}
		}

		.main-content {
			.label {
				font-weight: 600;
			}

			.main-content-main {
				font-size: 30rpx;
				font-family: PingFang SC;
				font-weight: 400;
				color: #666666;
			}

			.main-content-image {
				padding-top: 23rpx;

				image {
					width: 691rpx;
					height: 358rpx;
					border-radius: 5rpx;
				}
			}
		}

		.CommentCollection {
			display: flex;
			justify-content: flex-end;
			align-items: center;
			font-size: 22rpx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #999999;
			padding: 29rpx 0 23rpx 0;

			view {
				display: flex;
				align-items: center;
			}

			image {
				width: 28rpx;
				height: 26rpx;
				margin-right: 10rpx;
			}

			view:last-child {
				padding-left: 42rpx;
			}
		}
	}

	.comment {
		// height: 371px;
		background: #FFFFFF;
		margin-top: 20rpx;
		padding: 0 20rpx;
		margin-bottom: 100rpx;

		.comment-header {
			display: flex;
			align-items: center;
			padding-top: 33rpx;

			text {
				font-size: 30rpx;
				font-family: PingFang SC;
				font-weight: 400;
				color: #333333;
			}

			text:first-child {
				display: inline-block;
				width: 10rpx;
				height: 30rpx;
				background: #F14500;
				margin-right: 10rpx;
			}
		}

		.comment-content {
			padding-top: 18rpx;

			.comment-content-item {
				.comment-title {
					padding-top: 10rpx;
					display: flex;
					justify-content: space-between;
					align-items: center;
					border-bottom: 1px solid #E6E6E6;

					.comment-title-left {
						display: flex;
						// align-items: center;
						padding-left: 16rpx;

						.userImage {
							padding-right: 35rpx;

							image {
								width: 50rpx;
								height: 50rpx;
								border-radius: 50%;
							}
						}

						.user {
							.userName {
								font-size: 28rpx;
								font-family: PingFang SC;
								font-weight: 400;
								color: #666666;
							}

							.dateTime {
								font-size: 20rpx;
								font-family: PingFang SC;
								font-weight: 400;
								color: #666666;

								text:last-child {
									padding-left: 20rpx;
								}
							}

							.comment-title-main {
								font-size: 30rpx;
								font-family: PingFang SC;
								font-weight: 400;
								color: #333333;
								padding: 27rpx 0 18rpx 0;
							}
						}
					}

					.comment-title-right {
						font-size: 28rpx;
						font-family: PingFang SC;
						font-weight: 400;
						color: #F14500;
					}
				}

				.comment-main {
					padding: 27rpx 0 50rpx 0;

					.comment-main-left {
						text:first-child {
							font-size: 28rpx;
							font-family: PingFang;
							font-weight: 500;
							color: #666666;
							padding-right: 20rpx;
						}

						text:last-child {
							font-size: 30rpx;
							font-family: PingFang SC;
							font-weight: 400;
							color: #333333;
						}
					}

					.comment-main-right {
						display: flex;
						justify-content: flex-end;
						font-size: 20rpx;
						font-family: PingFang SC;
						font-weight: 400;
						color: #666666;

						text:last-child {
							padding-left: 20rpx;
						}
					}
				}
			}
		}
	}

	.footer {
		position: fixed;
		display: flex;
		justify-content: space-between;
		align-items: center;
		width: 750rpx;
		height: 88rpx;
		bottom: 0;
		left: 0;
		background: #FFFFFF;

		.footer-left {
			padding: 0 20rpx 0 43rpx;

			input {
				width: 537rpx;
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: 400;
			}
		}

		.footer-right {
			width: 150rpx;
			height: 88rpx;
			text-align: center;
			background: #F14500;
			font-size: 32rpx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #FFFFFF;
			line-height: 88rpx;
		}
	}
}
</style>